package com.ruoyi.system.service.api.patient.impl;

import com.ruoyi.system.domain.api.patient.*;
import com.ruoyi.system.mapper.api.patient.GroupApiMapper;
import com.ruoyi.system.mapper.api.patient.PatientInfoApiMapper;
import com.ruoyi.system.mapper.api.patient.RelationshipApiMapper;
import com.ruoyi.system.service.api.patient.IRelationshipApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import static com.ruoyi.system.util.CommonUtils.CurrentTime;


@Service
public class RelationshipApiServiceImpl implements IRelationshipApiService {
    @Autowired
    private RelationshipApiMapper relationshipApiMapper;

    @Autowired
    private GroupApiMapper groupApiMapper;

    @Autowired
    private PatientInfoApiMapper patientInfoApiMapper;

    private static final Logger log = LoggerFactory.getLogger(RelationshipApiServiceImpl.class);

    @Override
    public Integer addRelationship(RelationshipDto relationshipDto){
        // 判断医生ID在医生信息表中是否存在
        DoctorInfoDto doctorInfoDto = groupApiMapper.selectDoctorInfoByDocId(relationshipDto.getDoctorInfo().getDoctorId());
        if (doctorInfoDto == null) {
            log.info("查询医生信息警告:时间--{},医生ID:{},信息:{}", CurrentTime(), relationshipDto.getDoctorInfo().getDoctorId(), "医生未注册，暂无法关注！");
            //  todo 如果要想在关注中将医生注册到患者管理数据库中，就需要在如下添加医生新增逻辑，前端需要带有医生信息参数。否则直接返回医生未注册不可关注
            return 9001;
        }
        PatientInfoDto patientInfoDto = patientInfoApiMapper.selectPatientInfoByPatientId(relationshipDto.getPatientInfo().getPatientId());
        RelationDao relationDao = new RelationDao();
        if(patientInfoDto == null) {
            // 如果没有患者信息则新增患者相关信息到患者表
            patientInfoApiMapper.insertPatientInfo(relationshipDto.getPatientInfo());
            // 判断关系表内是否已经存在绑定关系，存在则更新不存在则新增
            // 暂时可不加事务
            relationDao.setPatientId(relationshipDto.getPatientInfo().getPatientId());
            relationDao.setDoctorId(relationshipDto.getDoctorInfo().getDoctorId());
            if (relationshipApiMapper.selectRelationshipByDocAPatId(relationDao) == null) {
                // 新增绑定关系
                return relationshipApiMapper.addRelationship(relationshipDto);
            }else{
                return relationshipApiMapper.updateRelationship(relationshipDto);

            }
        } else {
            // 判断关系表内是否已经存在绑定关系，存在则更新不存在则新增
            relationDao.setPatientId(relationshipDto.getPatientInfo().getPatientId());
            relationDao.setDoctorId(relationshipDto.getDoctorInfo().getDoctorId());
            if (relationshipApiMapper.selectRelationshipByDocAPatId(relationDao) == null) {
                // 新增绑定关系
                return relationshipApiMapper.addRelationship(relationshipDto);
            }else{
                // 更新绑定关系
                return relationshipApiMapper.updateRelationship(relationshipDto);

            }
        }

    }
    /**
     * 问诊关系绑定
     *
     * @author sergio
     * @date 2024/11/14
     */
    @Override
    public Integer addConsultation(RelationshipDto relationshipDto){
        // 判断医生ID在医生信息表中是否存在
        DoctorInfoDto doctorInfoDto = groupApiMapper.selectDoctorInfoByDocId(relationshipDto.getDoctorInfo().getDoctorId());
        if (doctorInfoDto == null) {
            log.info("查询医生信息警告:时间--{},医生ID:{},信息:{}", CurrentTime(), relationshipDto.getDoctorInfo().getDoctorId(), "医生未注册，暂无法关注！");
            //  todo 如果要想在关注中将医生注册到患者管理数据库中，就需要在如下添加医生新增逻辑，前端需要带有医生信息参数。否则直接返回医生未注册不可关注
            return 9001;
        }
        PatientInfoDto patientInfoDto = patientInfoApiMapper.selectPatientInfoByPatientId(relationshipDto.getPatientInfo().getPatientId());
        RelationDao relationDao = new RelationDao();
        if(patientInfoDto == null) {
            // 如果没有患者信息则新增患者相关信息到患者表
            patientInfoApiMapper.insertPatientInfo(relationshipDto.getPatientInfo());
            // 判断关系表内是否已经存在绑定关系，存在则更新不存在则新增
            // 暂时可不加事务
            relationDao.setPatientId(relationshipDto.getPatientInfo().getPatientId());
            relationDao.setDoctorId(relationshipDto.getDoctorInfo().getDoctorId());
            if (relationshipApiMapper.selectRelationshipByDocAPatId(relationDao) == null) {
                // 新增绑定关系
                return relationshipApiMapper.addRelationship(relationshipDto);
            }else{
                return relationshipApiMapper.updateRelationship(relationshipDto);

            }
        } else {
            // 判断关系表内是否已经存在绑定关系，存在则更新不存在则新增
            relationDao.setPatientId(relationshipDto.getPatientInfo().getPatientId());
            relationDao.setDoctorId(relationshipDto.getDoctorInfo().getDoctorId());
            if (relationshipApiMapper.selectRelationshipByDocAPatId(relationDao) == null) {
                // 新增绑定关系
                return relationshipApiMapper.addRelationship(relationshipDto);
            }else{
                // 更新绑定关系
                return relationshipApiMapper.updateRelationship(relationshipDto);

            }
        }

    }

}
